/**
 * 
 */
package ar.com.gtug.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ar.com.gtug.model.Event;
import ar.com.gtug.repository.EventRepository;
import ar.com.gtug.service.EventService;
import ar.com.gtug.util.LocationService;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

/**
 * @author Juan M. Irungaray (juan@irungaray.com.ar)
 *
 */
@Service
public class EventServiceImpl implements EventService {

//	@Autowired
//	private ChapterRepository chapterRepository;
	
	@Autowired
	private EventRepository repository;

	
//	public Event addEvent(long chapterId, String name, Date startDate,
//			Date endDate, List<Long> topics, int timezone, String address,
//			String country, String state, String city, String url,
//			String description) {
//		Chapter chapter = chapterRepository.get(chapterId);
//		Event event = new Event(name, startDate, endDate, topics, timezone, address, LocationService.getLocation(city, state, country), url, new Text(description));
//		chapter.addEvent(event);
//		chapterRepository.save(chapter);
//		return event;
//	}

	public void removeEvent(String eventId) {
		Key eventKey = KeyFactory.stringToKey(eventId);
		repository.delete(repository.get(eventKey));
	}

	public Event getEvent(String eventId) {
		Key eventKey = KeyFactory.stringToKey(eventId);
		return repository.get(eventKey);
	}

	public void editEvent(String eventId, String name,
			Date startDate, Date endDate, List<Long> topics, int timezone,
			String address, String country, String state, String city,
			String url, String description) {
		
		Key eventKey = KeyFactory.stringToKey(eventId);
		Event event= repository.get(eventKey);
		event.setName(name);
		event.setStartDate(startDate);
		event.setEndDate(endDate);
		event.setTopics(topics);
		event.setTimezone(timezone);
		event.setAddress(address);
		event.setLocation(LocationService.getLocation(city, state, country));
		event.setUrl(url);
		event.setDescription(new Text(description));
		repository.save(event);
		
	}
}
